package com.hw.servlet;

import com.google.gson.Gson;
import com.hw.Tool.ChangeJson;
import com.hw.dao.DepartmentsDao;
import com.hw.dao.EmployeeDao;
import com.hw.entity.Departments;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@WebServlet(name = "DepartServlet",urlPatterns = "/dp")
public class DepartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Enumeration<String> enumeration=request.getParameterNames();
        String json="";
        while (enumeration.hasMoreElements()){
           json=enumeration.nextElement();
        }
        Gson gson=new Gson();
        EmployeeDao employeeDao=new EmployeeDao();
        List<Double> list= (List<Double>) gson.fromJson(json,List.class);
        list.forEach((a)->{
            int aa=(int) Math.round(a);
            employeeDao.deleteById(aa);
        });



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();
        DepartmentsDao departmentsDao=new DepartmentsDao();
        Map<String,Object> data=new HashMap<>();
        List<Departments> departments=departmentsDao.findAll();
        data.put("depart",departments);
        String json=ChangeJson.toJson(data);
        out.write(json);
    }
}
